﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebGuiMVC.BllLessonService;

namespace WebGuiMVC.Controllers {

    /// <summary>
    /// The AdminController
    /// </summary>
    public class AdminController : Controller {
        
        /// <summary>
        /// The LessonServiceClient
        /// </summary>
        private LessonServiceClient lessons;

        /// <summary>
        /// Construct the AdminController
        /// </summary>
        public AdminController() {
            lessons = new LessonServiceClient();
        }

        /// <summary>
        /// Get the Index view
        /// </summary>
        /// <returns>The Index view</returns>
        public ActionResult Index() {
            return View();
        }

        /// <summary>
        /// Get the lessons as json
        /// </summary>
        /// <returns>The lessons as json</returns>
        public JsonResult GetLessons() {
            return Json(lessons.GetLessons().ToList(), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Get the NewLesson view
        /// </summary>
        /// <returns>The NewLesson view</returns>
        public ActionResult NewLesson() {
            return View();
        }

        /// <summary>
        /// Add a new lesson
        /// </summary>
        /// <param name="lesson">The lesson to add</param>
        /// <returns>The NewLesson view</returns>
        [HttpPost]
        public ActionResult NewLesson(BeLesson lesson) {
            if(ModelState.IsValid) {
                try {
                    lessons.SaveLesson(lesson);
                    return RedirectToAction("Index");
                } catch(Exception ex) {
                    ViewBag.exception = ex.Message;
                }
            }
            return View(lesson);
        }

        /// <summary>
        /// Edit a lesson with the id
        /// </summary>
        /// <param name="id">The id of the lesson</param>
        /// <returns>The EditLesson view</returns>
        public ActionResult EditLesson(int id) {
            BeLesson lesson = lessons.GetLesson(id);
            return View(lesson);
        }

        /// <summary>
        /// Edit a lesson
        /// </summary>
        /// <param name="lesson">The lesson to edit</param>
        /// <returns>Redirect to the Index view if the editing succeed.
        /// Otherwise it will return the EditLesson view</returns>
        [HttpPost]
        public ActionResult EditLesson(BeLesson lesson) {
            if(ModelState.IsValid) {
                lessons.EditLesson(lesson);
                return RedirectToAction("Index");
            }
            return View(lesson);
        }

        /// <summary>
        /// Delete a lesson with an id
        /// </summary>
        /// <param name="id">The id of the lesson</param>
        /// <returns>Redirect to the Index view if the deleting succeed</returns>
        public ActionResult DeleteLesson(int id) {
            if(ModelState.IsValid) {
                BeLesson lesson = lessons.GetLesson(id);
                lessons.DeleteLesson(lesson);
            }
            return RedirectToAction("Index");
        }
    }
}